package com.guangzi.common;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class PathUtils {

    public static String uploadDir(HttpServletRequest request) {
        String rootDir = request.getServletContext().getRealPath("/");
        File file = new File(rootDir, "/uploads");
        if (!file.exists()) {
            file.mkdirs();
            System.out.println("-------------------创建 uploads 目录成功");
        }
        System.out.println("--------------file.getAbsolutePath()-----创建 uploads 目录成功"+file.getAbsolutePath());
        return file.getAbsolutePath();
    }



    public static String getNewName( String fileName){
        Date current = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dataInfo = sdf.format(current);// 新的文件名字
        // 截取 获得源文件的 扩展名
        // 获取上传文件扩展名
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

        String newName = dataInfo+"."+fileExt; // 新的文件名称
        return newName;
    }
}
